package com.neuronbit.businessflow.flow.clazz;

import com.neuronbit.javaparser.resolution.types.ResolvedReferenceType;
import com.neuronbit.javaparser.resolution.types.ResolvedType;

/**
 * The type Source parameter type info.
 */
public class SourceParameterTypeInfo implements ParameterTypeInfo {
    private final ResolvedType resolvedType;

    /**
     * Instantiates a new Source parameter type info.
     *
     * @param type the type
     */
    public SourceParameterTypeInfo(ResolvedType type) {
        resolvedType = type;
    }

    @Override
    public boolean isAssignableTo(Class<?> exceptionClass) {
        if (getQualifiedName().equals(exceptionClass.getName())) {
            return true;
        }
        if (resolvedType.isReferenceType()) {
            ResolvedReferenceType resolvedType1 = (ResolvedReferenceType) resolvedType;
            if (resolvedType1.getQualifiedName().equals(exceptionClass.getCanonicalName())) {
                return true;
            }
            for (ResolvedReferenceType ancestor : resolvedType1.getAllAncestors()) {
                if (ancestor.getQualifiedName().equals(exceptionClass.getCanonicalName())) {
                    return true;
                }
            }
            return false;
        } else {
            throw new UnsupportedTypeException(resolvedType);
        }
    }

    @Override
    public boolean isAssignableTo(ParameterTypeInfo parameterType) {
        if (getQualifiedName().equals(parameterType.getQualifiedName())) {
            return true;
        }
        if (resolvedType.isReferenceType()) {
            ResolvedReferenceType resolvedType1 = (ResolvedReferenceType) resolvedType;
            if (resolvedType1.getQualifiedName().equals(parameterType.getQualifiedName())) {
                return true;
            }
            for (ResolvedReferenceType ancestor : resolvedType1.getAllAncestors()) {
                if (ancestor.getQualifiedName().equals(parameterType.getQualifiedName())) {
                    return true;
                }
            }
            return false;
        } else {
            throw new UnsupportedTypeException(resolvedType);
        }
    }

    @Override
    public String getQualifiedName() {
        if (this.resolvedType.isArray()) {
            return new SourceParameterTypeInfo(this.resolvedType.asArrayType().getComponentType()).getQualifiedName();
        }
        if (this.resolvedType.isPrimitive()) {
            return this.resolvedType.asPrimitive().name();
        }
        if (this.resolvedType.isReferenceType()) {
            return resolvedType.asReferenceType().getQualifiedName();
        }
        if (this.resolvedType.isTypeVariable()) {
            return this.resolvedType.asTypeParameter().getQualifiedName();
        }
        throw new UnsupportedTypeException(resolvedType);
    }
}
